package com.xiaoq.coms;

import java.util.List;

import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;

import io.swagger.annotations.ApiModelProperty;

@JsonAutoDetect(fieldVisibility = Visibility.NONE, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
@JsonInclude(value = JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class GenericListResponse<T> extends GenericResponse {
	@ApiModelProperty(name = "total", value = "总行数", position = 2)
	@JsonProperty("total")
	private long total;

	@ApiModelProperty(name = "list", value = "数据列表", position = 3)
	@JsonProperty("list")
	private List<T> list;

	public static ResponseEntity<?> listNoCount(List<?> arr) {
		GenericListResponse resp = new GenericListResponse();
		resp.setRc(CODE_OK);
		if (arr == null) {
			resp.setTotal(0);
		} else {
			resp.setTotal(arr.size());
		}
		resp.setList(arr);
		return ResponseEntity.ok(resp);
	}

	public static ResponseEntity<?> listNoCount(String rc, String msg,List<?> arr) {
		GenericListResponse resp = new GenericListResponse();
		resp.setRc(rc);
		resp.setMsg(msg);
		if (arr == null) {
			resp.setTotal(0);
		} else {
			resp.setTotal(arr.size());
		}
		resp.setList(arr);
		return ResponseEntity.ok(resp);
	}

	public static ResponseEntity<?> listAndCount(List<?> arr, Integer tc) {
		return listAndCount(arr, new Long(tc));
	}

	public static ResponseEntity<?> listAndCount(List<?> arr, Long tc) {
		GenericListResponse resp = new GenericListResponse();
		resp.setRc(CODE_OK);
		if (arr == null) {
			resp.setTotal(0);
		} else {
			resp.setList(arr);
			resp.setTotal(tc);
		}
		return ResponseEntity.ok(resp);
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

}
